package com.example.barcodeapp.ui.wode.activity;


import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.barcodeapp.R;
import com.example.barcodeapp.base.BaseActivity;
import com.example.barcodeapp.interfaces.IBasePersenter;


public class SetLoginPasswordActivity extends BaseActivity implements View.OnClickListener, TextWatcher {
    //最大字节数，不用区分是中文还是英文，字节数一定，可以动态插入字符个数(如：还可插入1个汉字，若是英文则还可以插入3个英文字母<手机显示一个汉字要3个字节,一个字母1个字节>)，一般此值为255, 2^8 -1
    private static final int MAX_LENGTH = 20;
    private EditText mEtPwd;
    private EditText mEtPwdTwo;
    private TextView mTvUpdatePwdEnter;
    private TextView mTvFinish;
    private String temp = null;//临时变量
    private String name = null;//改变后的字符串
    private String frontString = null;//frontString是字符串 1
    private String middleString = null;//字符串3
    private String behindString = null;//字符串2
    private int editStart = 0;
    private int editEnd = 0;
    private int startPos = 0;//光标的位置，即1/2之间的位置
    private int endPos = 0;//改变文字后光标的位置

    @Override
    protected int getLayout() {
        return R.layout.activity_set_login_password;
    }

    @Override
    protected void initView() {
        mEtPwd = findViewById(R.id.et_pwd);
        mEtPwdTwo = findViewById(R.id.et_pwd_two);
        mTvFinish = findViewById(R.id.tv_finish);
        mTvUpdatePwdEnter = findViewById(R.id.tv_update_pwd_enter);
        mEtPwd.setHintTextColor(Color.parseColor("#B6B6B6"));
        mEtPwdTwo.setHintTextColor(Color.parseColor("#B6B6B6"));
        initListener();
    }

    private void initListener() {
        mTvUpdatePwdEnter.setOnClickListener(this);
        mTvFinish.setOnClickListener(this);
        mEtPwd.addTextChangedListener(this);
    }

    @Override
    protected IBasePersenter initPersenter() {
        return null;
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_update_pwd_enter:
                String pwd = mEtPwd.getText().toString();
                String pwdTwo = mEtPwdTwo.getText().toString();
                if (!pwd.isEmpty()) {
                    if (!pwdTwo.isEmpty()) {
                        if (pwd.equals(pwdTwo)) {
                            Toast.makeText(SetLoginPasswordActivity.this, "密码设置成功", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(SetLoginPasswordActivity.this, "两次密码输入不一致", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(SetLoginPasswordActivity.this, "请再次输入密码", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(SetLoginPasswordActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.tv_finish:
                SetLoginPasswordActivity.this.finish();
                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        //此count和 beforeTextChanged方法中的count没有任何联系
        int count = 0;//定义为局部变量，防止上次的值对其有影响(小编刚开始定义为了全局变量，结果引发的错误排查了很久)
        name = s.toString().trim();//主要是为了获取name的字节长度，为了调试用
        editEnd = mEtPwd.getSelectionEnd();
        behindString = name.substring(editEnd, name.length());//behindString是字符串2
        middleString = name.substring(startPos, editEnd);//middleString是字符串3
        StringBuilder sb = new StringBuilder(temp);//根据改变前的字符串构键sb
        if (name.getBytes().length > MAX_LENGTH && middleString != null && middleString != "") {

        }
    }
}